#coding=utf-8

import logging

class Logger(object):
    def __init__(self, logger_name = 'logs'):
        self.logger = logging.getLogger(logger_name)
        self.logger.setLevel(logging.DEBUG)
        #logging.root.setLevel(logging.NOTSET)
        self.log_file_name = 'all.log'  # 日志文件的名称
        self.backup_count = 200  # 最多存放日志的数量
        # 日志输出级别
        self.console_output_level = 'WARNING'
        self.file_output_level = 'DEBUG'
        # 日志输出格式
        self.formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(lineno)d -  %(message)s')
        self.formatter_thread = logging.Formatter('%(asctime)s - [%(process)d]-[%(processName)s]:[%(thread)d]-[%(threadName)s]%(name)s - %(levelname)s - %(lineno)d -  %(message)s')


    def get_logger(self):
        if not self.logger.handlers:  # 避免重复日志
            file_handler = logging.FileHandler(self.log_file_name,encoding="utf-8")
            # file_handler = logging.handlers.RotatingFileHandler(self.log_file_name, maxBytes=102400, backupCount=5,)
            console_handler = logging.StreamHandler()

            console_handler.setFormatter(self.formatter_thread)
            console_handler.setLevel(self.file_output_level)
            self.logger.addHandler(file_handler)
            self.logger.addHandler(console_handler)

            # 每天重新创建一个日志文件，最多保留backup_count份
            # file_handler = TimedRotatingFileHandler(filename=os.path.join(log_path, self.log_file_name), when='D',
            #                                         interval=1, backupCount=self.backup_count, delay=True,
            #                                         encoding='utf-8')
            # file_handler.setFormatter(self.formatter)
            # file_handler.setLevel(self.file_output_level)
            # self.logger.addHandler(file_handler)
        return self.logger


logger = Logger().get_logger()